package orm.armory.dao
{
	/**
	* @author www.comtaste.com
	*/
	
	import flash.data.SQLConnection;
	import flash.events.SQLEvent;
	import flash.events.SQLErrorEvent;
	import flash.data.SQLStatement;
	import flash.errors.SQLError;
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	
	import orm.armory.vo.TblItemPropertyVO;
	
	public class TblItemProperty_DAO
	{
	
		
		private static var instance:TblItemProperty_DAO;
		public static function getInstance():TblItemProperty_DAO {
			if( instance == null )
				instance = new TblItemProperty_DAO( new SingletonLock );
			return instance;
		}
		
	
		public function TblItemProperty_DAO( lock: SingletonLock) {
		}
	
		private var sqlConnection:SQLConnection;
		public function getConnection():SQLConnection {
			return sqlConnection;
		}
		public function setConnection( connection:SQLConnection, initializeTable:Boolean = false ):void {
			// store connection reference
			sqlConnection = connection;
			// try construct table on Database any time a new connection is submitted
			if(sqlConnection.connected && initializeTable){
				createTable();
				
			}
		}
	
		public function createTable( resultHandler:Function = null, faultHandler:Function = null ):void {
			var stmt:SQLStatement = new SQLStatement();
			stmt.sqlConnection = sqlConnection;
			stmt.text = 'CREATE TABLE IF NOT EXISTS TblItemProperty (id INTEGER PRIMARY KEY AUTOINCREMENT,     info_item_id TEXT,     info_bind_type TEXT,     info_unique_type TEXT,     weapon_min_damage INTEGER,     weapon_max_damage INTEGER,     base_armor INTEGER,     base_strenth INTEGER,     base_agility INTEGER,     base_stamina INTEGER,     base_intellect INTEGER,     base_spirit INTEGER,     general_crit_rating INTEGER,     general_hit_rating INTEGER,     general_haste_rating INTEGER,     physical_attack_power INTEGER,     physical_armor_penetration INTEGER,     physical_expertise_rating INTEGER,     spell_power INTEGER,     spell_penetration INTEGER,     spell_mana_regen INTEGER,     defense_block_rating INTEGER,     defense_block_value INTEGER,     defense_defense_rating INTEGER,     defense_dodge_rating INTEGER,     defense_parry_rating INTEGER,     defense_health_regen INTEGER,     defense_resilience INTEGER,     resistance_arcane INTEGER,     resistance_shadow INTEGER,     resistance_nature INTEGER,     resistance_frost INTEGER,     resistance_fire INTEGER,     addition_effect TEXT,     info_side TEXT,     info_icon TEXT,     info_item_name TEXT,     info_item_level TEXT,     weapon_dps NUMERIC,     weapon_speed NUMERIC,     socket_meta INTEGER,     socket_red INTEGER,     socket_yellow INTEGER,     socket_blue INTEGER, socket_bonus TEXT);';
			stmt.addEventListener( SQLEvent.RESULT,
			function ( event:SQLEvent ):void {
				if (resultHandler != null) resultHandler.call(this);
			});
			stmt.addEventListener( SQLErrorEvent.ERROR, faultHandler == null ? sqlErrorHandler : faultHandler );
			stmt.execute();
		}
		
		public function getTableContent( resultHandler:Function, faultHandler:Function = null ):void {
			var stmt:SQLStatement = new SQLStatement();
			stmt.sqlConnection = sqlConnection;
			stmt.text = 'SELECT * FROM TblItemProperty;';
			stmt.itemClass = TblItemPropertyVO;
			stmt.addEventListener( SQLEvent.RESULT,
			function ( event:SQLEvent ):void {
				resultHandler.call( this, new ArrayCollection( stmt.getResult().data ) );
			});
			stmt.addEventListener( SQLErrorEvent.ERROR, faultHandler == null ? sqlErrorHandler : faultHandler );
			stmt.execute();
		}
		
		public function updateRow( rowItem:TblItemPropertyVO, resultHandler:Function = null, faultHandler:Function = null ):void {
			var stmt:SQLStatement = new SQLStatement();
			stmt.sqlConnection = sqlConnection;
			stmt.text = 'UPDATE TblItemProperty SET id = @id, info_item_id = @info_item_id, info_bind_type = @info_bind_type, info_unique_type = @info_unique_type, weapon_min_damage = @weapon_min_damage, weapon_max_damage = @weapon_max_damage, base_armor = @base_armor, base_strenth = @base_strenth, base_agility = @base_agility, base_stamina = @base_stamina, base_intellect = @base_intellect, base_spirit = @base_spirit, general_crit_rating = @general_crit_rating, general_hit_rating = @general_hit_rating, general_haste_rating = @general_haste_rating, physical_attack_power = @physical_attack_power, physical_armor_penetration = @physical_armor_penetration, physical_expertise_rating = @physical_expertise_rating, spell_power = @spell_power, spell_penetration = @spell_penetration, spell_mana_regen = @spell_mana_regen, defense_block_rating = @defense_block_rating, defense_block_value = @defense_block_value, defense_defense_rating = @defense_defense_rating, defense_dodge_rating = @defense_dodge_rating, defense_parry_rating = @defense_parry_rating, defense_health_regen = @defense_health_regen, defense_resilience = @defense_resilience, resistance_arcane = @resistance_arcane, resistance_shadow = @resistance_shadow, resistance_nature = @resistance_nature, resistance_frost = @resistance_frost, resistance_fire = @resistance_fire, addition_effect = @addition_effect, info_side = @info_side, info_icon = @info_icon, info_item_name = @info_item_name, info_item_level = @info_item_level, weapon_dps = @weapon_dps, weapon_speed = @weapon_speed, socket_meta = @socket_meta, socket_red = @socket_red, socket_yellow = @socket_yellow, socket_blue = @socket_blue, socket_bonus = @socket_bonusWHERE ID = ?;';
			setParameters( stmt, [ {name:"id", value:rowItem.id}, {name:"info_item_id", value:rowItem.info_item_id}, {name:"info_bind_type", value:rowItem.info_bind_type}, {name:"info_unique_type", value:rowItem.info_unique_type}, {name:"weapon_min_damage", value:rowItem.weapon_min_damage}, {name:"weapon_max_damage", value:rowItem.weapon_max_damage}, {name:"base_armor", value:rowItem.base_armor}, {name:"base_strenth", value:rowItem.base_strenth}, {name:"base_agility", value:rowItem.base_agility}, {name:"base_stamina", value:rowItem.base_stamina}, {name:"base_intellect", value:rowItem.base_intellect}, {name:"base_spirit", value:rowItem.base_spirit}, {name:"general_crit_rating", value:rowItem.general_crit_rating}, {name:"general_hit_rating", value:rowItem.general_hit_rating}, {name:"general_haste_rating", value:rowItem.general_haste_rating}, {name:"physical_attack_power", value:rowItem.physical_attack_power}, {name:"physical_armor_penetration", value:rowItem.physical_armor_penetration}, {name:"physical_expertise_rating", value:rowItem.physical_expertise_rating}, {name:"spell_power", value:rowItem.spell_power}, {name:"spell_penetration", value:rowItem.spell_penetration}, {name:"spell_mana_regen", value:rowItem.spell_mana_regen}, {name:"defense_block_rating", value:rowItem.defense_block_rating}, {name:"defense_block_value", value:rowItem.defense_block_value}, {name:"defense_defense_rating", value:rowItem.defense_defense_rating}, {name:"defense_dodge_rating", value:rowItem.defense_dodge_rating}, {name:"defense_parry_rating", value:rowItem.defense_parry_rating}, {name:"defense_health_regen", value:rowItem.defense_health_regen}, {name:"defense_resilience", value:rowItem.defense_resilience}, {name:"resistance_arcane", value:rowItem.resistance_arcane}, {name:"resistance_shadow", value:rowItem.resistance_shadow}, {name:"resistance_nature", value:rowItem.resistance_nature}, {name:"resistance_frost", value:rowItem.resistance_frost}, {name:"resistance_fire", value:rowItem.resistance_fire}, {name:"addition_effect", value:rowItem.addition_effect}, {name:"info_side", value:rowItem.info_side}, {name:"info_icon", value:rowItem.info_icon}, {name:"info_item_name", value:rowItem.info_item_name}, {name:"info_item_level", value:rowItem.info_item_level}, {name:"weapon_dps", value:rowItem.weapon_dps}, {name:"weapon_speed", value:rowItem.weapon_speed}, {name:"socket_meta", value:rowItem.socket_meta}, {name:"socket_red", value:rowItem.socket_red}, {name:"socket_yellow", value:rowItem.socket_yellow}, {name:"socket_blue", value:rowItem.socket_blue}, {name:"socket_bonus", value:rowItem.socket_bonus} ] );
			stmt.addEventListener( SQLEvent.RESULT,
			function ( event:SQLEvent ):void {
				if ( resultHandler != null ) resultHandler.call( this, rowItem );
			});
			stmt.addEventListener( SQLErrorEvent.ERROR, faultHandler == null ? sqlErrorHandler : faultHandler );
			stmt.execute();
		}
		
		public function insertRow( rowItem:TblItemPropertyVO, resultHandler:Function = null, faultHandler:Function = null ):void {
			var stmt:SQLStatement = new SQLStatement();
			stmt.sqlConnection = sqlConnection;
			stmt.text = 'INSERT INTO TblItemProperty( info_item_id, info_bind_type, info_unique_type, weapon_min_damage, weapon_max_damage, base_armor, base_strenth, base_agility, base_stamina, base_intellect, base_spirit, general_crit_rating, general_hit_rating, general_haste_rating, physical_attack_power, physical_armor_penetration, physical_expertise_rating, spell_power, spell_penetration, spell_mana_regen, defense_block_rating, defense_block_value, defense_defense_rating, defense_dodge_rating, defense_parry_rating, defense_health_regen, defense_resilience, resistance_arcane, resistance_shadow, resistance_nature, resistance_frost, resistance_fire, addition_effect, info_side, info_icon, info_item_name, info_item_level, weapon_dps, weapon_speed, socket_meta, socket_red, socket_yellow, socket_blue, socket_bonus ) VALUES ( @info_item_id,@info_bind_type,@info_unique_type,@weapon_min_damage,@weapon_max_damage,@base_armor,@base_strenth,@base_agility,@base_stamina,@base_intellect,@base_spirit,@general_crit_rating,@general_hit_rating,@general_haste_rating,@physical_attack_power,@physical_armor_penetration,@physical_expertise_rating,@spell_power,@spell_penetration,@spell_mana_regen,@defense_block_rating,@defense_block_value,@defense_defense_rating,@defense_dodge_rating,@defense_parry_rating,@defense_health_regen,@defense_resilience,@resistance_arcane,@resistance_shadow,@resistance_nature,@resistance_frost,@resistance_fire,@addition_effect,@info_side,@info_icon,@info_item_name,@info_item_level,@weapon_dps,@weapon_speed,@socket_meta,@socket_red,@socket_yellow,@socket_blue,@socket_bonus );';
			var params:Array = [ {name:"info_item_id", value:rowItem.info_item_id}, {name:"info_bind_type", value:rowItem.info_bind_type}, {name:"info_unique_type", value:rowItem.info_unique_type}, {name:"weapon_min_damage", value:rowItem.weapon_min_damage}, {name:"weapon_max_damage", value:rowItem.weapon_max_damage}, {name:"base_armor", value:rowItem.base_armor}, {name:"base_strenth", value:rowItem.base_strenth}, {name:"base_agility", value:rowItem.base_agility}, {name:"base_stamina", value:rowItem.base_stamina}, {name:"base_intellect", value:rowItem.base_intellect}, {name:"base_spirit", value:rowItem.base_spirit}, {name:"general_crit_rating", value:rowItem.general_crit_rating}, {name:"general_hit_rating", value:rowItem.general_hit_rating}, {name:"general_haste_rating", value:rowItem.general_haste_rating}, {name:"physical_attack_power", value:rowItem.physical_attack_power}, {name:"physical_armor_penetration", value:rowItem.physical_armor_penetration}, {name:"physical_expertise_rating", value:rowItem.physical_expertise_rating}, {name:"spell_power", value:rowItem.spell_power}, {name:"spell_penetration", value:rowItem.spell_penetration}, {name:"spell_mana_regen", value:rowItem.spell_mana_regen}, {name:"defense_block_rating", value:rowItem.defense_block_rating}, {name:"defense_block_value", value:rowItem.defense_block_value}, {name:"defense_defense_rating", value:rowItem.defense_defense_rating}, {name:"defense_dodge_rating", value:rowItem.defense_dodge_rating}, {name:"defense_parry_rating", value:rowItem.defense_parry_rating}, {name:"defense_health_regen", value:rowItem.defense_health_regen}, {name:"defense_resilience", value:rowItem.defense_resilience}, {name:"resistance_arcane", value:rowItem.resistance_arcane}, {name:"resistance_shadow", value:rowItem.resistance_shadow}, {name:"resistance_nature", value:rowItem.resistance_nature}, {name:"resistance_frost", value:rowItem.resistance_frost}, {name:"resistance_fire", value:rowItem.resistance_fire}, {name:"addition_effect", value:rowItem.addition_effect}, {name:"info_side", value:rowItem.info_side}, {name:"info_icon", value:rowItem.info_icon}, {name:"info_item_name", value:rowItem.info_item_name}, {name:"info_item_level", value:rowItem.info_item_level}, {name:"weapon_dps", value:rowItem.weapon_dps}, {name:"weapon_speed", value:rowItem.weapon_speed}, {name:"socket_meta", value:rowItem.socket_meta}, {name:"socket_red", value:rowItem.socket_red}, {name:"socket_yellow", value:rowItem.socket_yellow}, {name:"socket_blue", value:rowItem.socket_blue}, {name:"socket_bonus", value:rowItem.socket_bonus} ];
			setParameters( stmt, params );
			stmt.addEventListener( SQLEvent.RESULT,
			function ( event:SQLEvent ):void {
				if (!rowItem.id > 0) rowItem.id = stmt.getResult().lastInsertRowID;
				if (resultHandler != null) resultHandler.call(this, rowItem);
			});
			stmt.addEventListener( SQLErrorEvent.ERROR, faultHandler == null ? sqlErrorHandler : faultHandler );
			stmt.execute();
		}
		
		public function deleteRow( rowItem:TblItemPropertyVO, resultHandler:Function = null, faultHandler:Function = null ):void {
			var stmt:SQLStatement = new SQLStatement();
			stmt.sqlConnection = sqlConnection;
			stmt.text = 'DELETE FROM TblItemProperty WHERE TblItemProperty.id = @id;';
			var params:Array = [ {name:"id", value:rowItem.id} ];
			setParameters( stmt, params );
			stmt.addEventListener( SQLEvent.RESULT,
			function ( event:SQLEvent ):void {
				if (resultHandler != null) resultHandler.call(this, rowItem);
			});
			stmt.addEventListener( SQLErrorEvent.ERROR, faultHandler == null ? sqlErrorHandler : faultHandler );
			stmt.execute();
		}
		
		private function setParameters( stmt:SQLStatement, params:Array ):void {
			var param:Object;
			for ( var i:int = 0; i < params.length; i++ ) {
				param = params[i];
				stmt.parameters[ '@' + param.name ] = param.value;
			}
		}
		
		private function sqlErrorHandler( event:SQLError ):void {
			Alert.show( event.message, "Error" );
		}
		
	}
}

class SingletonLock {}
